﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;

namespace Department.admin.SiteStructure
{
    public partial class ViewAllPages : System.Web.UI.Page
    {
        bool wasSortedDesc = AdminVariablesSingleton.WasSortedDesc;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                Response.Redirect("/admin/Login.aspx");
            }
            this.Page.Title = "Всички страници";

            AdminVariablesSingleton.changeCSS(Master, "struct");

            Control foundControl = AdminVariablesSingleton.FindControlRecursive(Master, "viewPages");
            ((HtmlControl)foundControl).Attributes.Add("class", "sub_show");
        }
       
        protected void sortByParam_Click(object sender, CommandEventArgs e)
        {
            string item = e.CommandArgument.ToString();

            string sqlText;
            if (wasSortedDesc)
            {
                sqlText = "SELECT P.ID, P.Name, P.IsMain, M.Name AS MenuName FROM Pages AS P INNER JOIN Menu AS M ON P.MenuID=M.ID ORDER BY " +item + " DESC";
                AdminVariablesSingleton.WasSortedDesc = false;
            }
            else
            {
                sqlText = "SELECT P.ID, P.Name, P.IsMain, M.Name AS MenuName FROM Pages AS P INNER JOIN Menu AS M ON P.MenuID=M.ID ORDER BY " + item;
                AdminVariablesSingleton.WasSortedDesc = true;
            }

            ListPagesDataSource.SelectCommand = sqlText;
            ListPages.DataBind();

        }

        protected void toggleall_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkHeader = (CheckBox)sender;
            for (int i = 0; i < ListPages.Items.Count; i++)
            {
                CheckBox chk = (CheckBox)ListPages.Items[i].FindControl("CheckBoxIsMarked");
                chk.Checked = chkHeader.Checked;
            }
        }        
    }
}